﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.CBO.Pub.Controller;

namespace ufida.u9.ait.openapi.utils
{
    public class ResultUtils
    {
        const string snMsg = "代码已经失效";
        static bool IsValid = false;

        static ResultUtils()
        {
            IsValid = U9Utils.IsValid();
        }

        public static T parseData<T>(ResultDTOData result)
        {
            if (result==null||string.IsNullOrEmpty(result.Data.ToString()))
                return default(T);
            return JsonConvert.DeserializeObject<T>(result.Data.ToString());
        }

        public static List<CommonArchiveDataDTOData> parseData2CommonArchive(ResultDTOData result)
        {
            if (result == null || string.IsNullOrEmpty(result.Data.ToString()))
                return null;
            return JsonConvert.DeserializeObject<List<CommonArchiveDataDTOData>>(result.Data.ToString());
        }

        public static ResultDTOData success()
        {
            return success(null);
        }
        /// <summary>
        /// 返回成功信息,但不记录数据库调用日志
        /// </summary>
        /// <param name="data">返回对象</param>
        /// <returns></returns>
        public static ResultDTOData success(Object data)
        {
            if (IsValid)
            {
                ResultDTOData ret = new ResultDTOData();
                ret.Code = 200;
                ret.Msg = "成功";
                string json = JsonConvert.SerializeObject(data);
                ret.Data = json;
                return ret;
            }
            else
            {
                return error();
            }
            
        }

        /// <summary>
        /// 返回成功信息,记录数据库调用日志
        /// 调用示例:
        /// ResultUtils.success(bpObj, bpObj.Data, retlist);
        /// </summary>
        /// <param name="method">方法对象</param>
        /// <param name="indata">方法参数</param>
        /// <param name="retdata">返回对象</param>
        /// <returns></returns>
        public static ResultDTOData success(object method,object indata,object retdata)
        {
            ResultDTOData ret=success(retdata);
            if (method != null)
            {
                U9Utils.DBLog(method, indata, ret);
            }
            return ret;
        }

        public static ResultDTOData error()
        {
            return error("失败", null);
        }

        public static ResultDTOData error(String msg)
        {
            return error(msg, null);
        }
        public static ResultDTOData error(String msg,object data)
        {
            return error(500,msg,data);
        }
        /// <summary>
        /// 返回错误信息,但不记录数据库调用日志
        /// </summary>
        /// <param name="code"></param>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ResultDTOData error(int code,String msg,object data)
        {
            ResultDTOData ret = new ResultDTOData();
            ret.Code = code;
            if (IsValid)
            {
                ret.Msg = msg;
                ret.Data = data;
            }
            else
            {
                ret.Msg = snMsg;
                ret.Data = null;
            }
            return ret;
        }
        /// <summary>
        /// 返回错误信息,并记录数据库调用日志
        /// </summary>
        /// <param name="method">方法对象</param>
        /// <param name="indata">方法参数</param>
        /// <param name="msg">错误消息</param>
        /// <param name="data">返回对象</param>
        /// <returns></returns>
        public static ResultDTOData error(object method,object indata,string msg,object data)
        {
            ResultDTOData ret = error(msg, data);
            if (method != null)
            {
                U9Utils.DBLog(method, indata, ret);
            }
            return ret;
        }
        public static ResultDTOData error(Exception ex)
        {
            ResultDTOData ret;
            if (ex is AITException)
            {
                if ((ex as AITException).ErrorCode == 599)
                {
                    ret= ResultUtils.error(599, ex.Message, ex.StackTrace);
                }
                else
                {
                    ret= ResultUtils.error(ex.Message, ex.StackTrace);
                }
            }
            else
            {
                ret= ResultUtils.error(ex.Message, ex.StackTrace);
            }

            return ret;
        }
        /// <summary>
        /// 服务返回值检查
        /// </summary>
        /// <param name="resultDTOData"></param>
        /// <exception cref="Exception">返回500时,直接报错</exception>
        public static void CheckResult(ResultDTOData resultDTOData)
        {
            if (resultDTOData.Code != 200)
                throw new Exception(resultDTOData.Msg);
        }

        /// <summary>
        /// 返回错误信息,并记录数据库调用日志
        /// </summary>
        /// <param name="method">方法对象</param>
        /// <param name="indata">方法参数</param>
        /// <param name="ex">异常错误</param>
        /// <returns></returns>
        public static ResultDTOData error(object method,object indata,Exception ex)
        {
            ResultDTOData ret = error(ex);
            if(method != null)
            {
                U9Utils.DBLog(method, indata, ret);
            }
            return ret;
        }
    }
}
